package com.mtw.bbs.bbsUser.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mtw.bbs.article.feign.LikeClient;
import com.mtw.bbs.bbsUser.pojo.entity.User;
import com.mtw.bbs.bbsUser.pojo.form.PasswordForm;
import com.mtw.bbs.bbsUser.pojo.form.UserUpdateForm;
import com.mtw.bbs.bbsUser.pojo.vo.UserCountVO;
import com.mtw.bbs.bbsUser.service.UserFollowService;
import com.mtw.bbs.common.core.security.UserDetails;
import com.mtw.bbs.common.core.security.context.SecurityContextHolder;
import com.mtw.bbs.common.core.util.CommonUtil;
import com.mtw.bbs.userAdmin.pojo.form.UserRegisterForm;
import com.mtw.bbs.common.core.vo.Result;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.mtw.bbs.bbsUser.service.UserService;

/**
 * 用户接口层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("user")
public class UserController {

    private final LikeClient likeClient;
    private final UserService userService;
    private final UserFollowService userFollowService;


    /**
     * 获取当前用户的关注数与粉丝数和点赞数
     *
     */
    @GetMapping(value = "/ffl_count")
    public Result getUserFollowLikeAndFansCount() {

        UserCountVO vo = new UserCountVO();
        UserDetails user = SecurityContextHolder.getContext().getAuthentication();
        if (user == null) {
            return Result.success(vo);
        }
        Result<Integer> userLikeCount = likeClient.getUserLikeCount(user.getUsername());
        if (userLikeCount.isSuccess()) {
            vo.setLike(userLikeCount.getData());
        }
        Integer follow = userFollowService.countOfFollow(user.getId());
        Integer fans = userFollowService.countOfFans(user.getId());
        vo.setFans(fans);
        vo.setFollow(follow);
        return Result.success(vo);
    }








    @PutMapping("/update")
    @ApiOperation(value = "用户修改个人信息", notes = "表单")
    public Result update(@RequestBody UserUpdateForm form) {

        String result = userService.updateUserInfo(form);
        if(StringUtils.isNotBlank(result)){
            return Result.fail(result);
        }
        return Result.success();
    }



    @PutMapping("/updateMobil")
    @ApiOperation(value = "用户修改手机号", notes = "表单")
    public Result updateMobil(@RequestBody UserRegisterForm form) {

        UserDetails currentUser = SecurityContextHolder.getContext().getAuthentication();
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.ne(User::getId,currentUser.getId());
        lqw.eq(User::getPhone,form.getPhone());
        long count = userService.count(lqw);
        if (count>0){
            return Result.fail(form.getPhone()+"已被使用");
        }

        LambdaUpdateWrapper<User> luw = new LambdaUpdateWrapper<>();
        luw.set(User::getPhone,form.getPhone());
        luw.eq(User::getId,currentUser.getId());
        userService.update(luw);

        return Result.success();
    }



    @PutMapping("/updateEmail")
    @ApiOperation(value = "用户修改邮箱", notes = "表单")
    public Result updateEmail(@RequestBody UserRegisterForm form) {

        UserDetails currentUser = SecurityContextHolder.getContext().getAuthentication();
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.ne(User::getId,currentUser.getId());
        lqw.eq(User::getEmail,form.getEmail());
        long count = userService.count(lqw);
        if (count>0){
            return Result.fail(form.getEmail()+"已被使用");
        }

        LambdaUpdateWrapper<User> luw = new LambdaUpdateWrapper<>();
        luw.set(User::getEmail,form.getEmail());
        luw.eq(User::getId,currentUser.getId());
        userService.update(luw);

        return Result.success();
    }


    @PutMapping("/updatePassword")
    @ApiOperation(value = "用户修改密码", notes = "传入密码表单")
    public Result updatePassword(@RequestBody PasswordForm form) {

        UserDetails currentUser = SecurityContextHolder.getContext().getAuthentication();

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getId,currentUser.getId());
        lqw.eq(User::getPassword, CommonUtil.getMD5(form.getOldPassword()));

        long count = userService.count(lqw);
        if (count < 1){
            return Result.fail("密码错误");
        }


        LambdaUpdateWrapper<User> luw = new LambdaUpdateWrapper<>();
        luw.set(User::getPassword, CommonUtil.getMD5(form.getNewPassword()));
        luw.eq(User::getId, currentUser.getId());

        boolean update = userService.update(luw);
        return update?Result.success():Result.fail("修改失败");
    }






}
